<template>
  <div class="container">
    <div>
      <el-button
        type="primary"
        size="mini"
        icon="el-icon-plus"
        @click="openDialog('新增')"
      >新增</el-button>
      <el-button
        type="danger"
        size="mini"
        class="el-icon-delete"
        :disabled="multipleSelection.length == 0"
        @click="deletes()"
      >删除</el-button>
      <el-button
        type="warning"
        size="mini"
        class="el-icon-download"
      >导出</el-button>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children'}"
    >
      <el-table-column
        label="部门名称"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.deptName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.isBanned == "0" ? "正常" : "禁用" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdOn) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            icon="el-icon-edit"
            @click="openEtidDialog('修改', scope.row)"
          >编辑</el-button>
          <el-button
            type="text"
            size="mini"
            icon="el-icon-delete"
            @click="deleteRow(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="dialogSetting.title"
      :visible.sync="dialogSetting.dialogVisible"
      :width="dialogSetting.width"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      :before-close="beforeCloseDialog"
    >
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        label-width="80px"
        :rules="rules"
      >
        <el-form-item
          v-if="dialogSetting.title === '新增'"
          label="上级部门"
          prop="parentId"
        >
          <treeselect
            v-model="ruleForm.parentId"
            :options="deptOptions"
            :clearable="false"
            :close-on-select="true"
            placeholder="请选择部门"
            :flatten-search-results="true"
            :normalizer="normalizer"
          />
        </el-form-item>
        <el-form-item
          label="部门名称"
          prop="deptName"
        >
          <el-input v-model="ruleForm.deptName" />
        </el-form-item>
        <el-form-item
          label="排序"
          prop="sort"
        >
          <el-input-number
            v-model="ruleForm.sort"
            size="medium"
            :step-strictly="true"
            controls-position="right"
            :min="1"
            :max="10"
            label="描述文字"
          />
        </el-form-item>
        <el-form-item
          v-if="dialogSetting.title === '修改'"
          label="状态"
          prop="isBanned"
        >
          <el-radio-group v-model="ruleForm.isBanned">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="备注"
          prop="remark"
        >
          <el-input
            v-model="ruleForm.remark"
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          size="small"
          @click="closeDialog()"
        >取 消</el-button>
        <el-button
          type="primary"
          size="small"
          @click="sub('ruleForm')"
        >确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import {
  getListTree,
  create,
  deptDetailById,
  update,
  deleteDept
} from '@/api/upms/dept'
// import the component
import Treeselect from '@riophae/vue-treeselect'
// import the styles
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: { Treeselect },
  data() {
    return {
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        jobName: '',
        isBanned: ''
      },
      tableData: [],
      total: 0,
      multipleSelection: [], // 选项集合
      deptOptions: [],
      dialogSetting: {
        dialogVisible: false,
        title: '',
        width: '30%'
      },
      ruleForm: {
        id: null,
        deptName: '',
        sort: 1,
        parentId: '0',
        remark: '',
        isBanned: '0'
      },
      rules: {
        parentId: [
          { required: true, message: '请选择上级部门', trigger: 'blur' }
        ],
        deptName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        sort: [{ required: true, message: '请输入排序', trigger: 'change' }],
        isBanned: [{ required: true, message: '请选择状态', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getListTree()
  },
  methods: {
    getDetail(row) {
      deptDetailById({ id: row.id }).then(resp => {
        if (resp.code === 0) {
          const rowDetail = resp.data.detail
          this.ruleForm.id = rowDetail.id
          this.ruleForm.deptName = rowDetail.deptName
          this.ruleForm.sort = rowDetail.sort
          this.ruleForm.parentId = rowDetail.parentId
          this.ruleForm.remark = rowDetail.remark
          this.ruleForm.isBanned = rowDetail.isBanned
          this.dialogSetting.title = '修改'
          this.dialogSetting.dialogVisible = true
        }
      })
    },
    deleteRow(row) {
      deleteDept(row.id).then(resp => {
        if (resp.code === 0) {
          this.getListTree()
        }
      })
    },
    openEtidDialog(title, rowId) {
      // 获取详情
      this.getDetail(rowId)
    },
    getListTree() {
      getListTree().then(resp => {
        if (resp.code === 0) {
          this.tableData = resp.data.list
          this.deptOptions = []
          const dept = { id: '0', deptName: '根节点', children: [] }
          dept.children = resp.data.list
          this.deptOptions.push(dept)
        }
      })
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.deptName,
        children: node.children
      }
    },
    openDialog(title) {
      this.dialogSetting.title = title
      this.dialogSetting.dialogVisible = true
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    beforeCloseDialog(done) {
      this.resetForm('ruleForm')
      this.reset()
      done()
    },
    closeDialog() {
      this.dialogSetting.dialogVisible = false
      this.resetForm('ruleForm')
      this.reset()
    },
    reset() {
      this.ruleForm.id = null
      this.ruleForm.deptName = ''
      this.ruleForm.sort = ''
      this.ruleForm.parentId = '0'
      this.ruleForm.isBanned = '0'
      this.ruleForm.remark = ''
    },
    sub(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          switch (this.dialogSetting.title) {
            case '新增':
              var createParam = {
                deptName: this.ruleForm.deptName,
                sort: this.ruleForm.sort,
                parentId: this.ruleForm.parentId,
                remark: this.ruleForm.remark
              }
              create(createParam).then(resp => {
                if (resp.code === 0) {
                  this.notifySuccess('创建成功')
                  this.closeDialog()
                  this.getListTree()
                }
              })
              break
            case '修改':
              var updateParam = {
                id: this.ruleForm.id,
                deptName: this.ruleForm.deptName,
                sort: this.ruleForm.sort,
                isBanned: this.ruleForm.isBanned,
                remark: this.ruleForm.remark
              }
              update(updateParam).then(resp => {
                if (resp.code === 0) {
                  this.notifySuccess('修改成功')
                  this.closeDialog()
                  this.getListTree()
                }
              })
              break
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "~@/styles/common.scss";
</style>
